package com.deliinsns.app.security;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.deliinsns.app.employer.bean.Employer;
import com.deliinsns.app.student.bean.Student;
import com.deliinsns.sys.BasicController;
import com.deliinsns.sys.MsgType;
import com.deliinsns.sys.util.SystemConstant;

@Controller
public class CheckcodeController extends BasicController {
	String[] arrs = { "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L",
			"M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
			"Z", "a", "b", "c", "d", "e", "f", "g", "h", "j", "k", "l", "m",
			"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
			"1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
	
	@RequestMapping(value = "checkcode/{checkcode}", method = RequestMethod.GET)
	 public void validateCheckcode(@PathVariable String checkcode, HttpServletRequest request,HttpServletResponse response) throws Exception {
		JSONObject json = new JSONObject();
		String code= (String) request.getSession().getAttribute("code");
		int state=202;
		if(checkParam(checkcode,code)){	
			if(checkcode.equalsIgnoreCase(code)){
				state= 200;
			}
		}
		json.put("state", state);
		out(request,response,json);
	}
	@RequestMapping(value = "checkcode", method = RequestMethod.GET)
	 public void checkcode(HttpServletRequest request,HttpServletResponse response) throws Exception {
		// 用代码画图
		// 做一个画板
		int width = 800;
		int height = 300;
		BufferedImage image = new BufferedImage(width, height,
				BufferedImage.TYPE_INT_RGB);
		Graphics p = image.getGraphics();
		p.setColor(getRandomColor());
		p.fillRect(0, 0, width, height);
		// 画圆
		Random ran = new Random();
		for (int i = 0; i < 1000; i++) {
			p.setColor(getRandomColor());
			int x = ran.nextInt(width);
			int y = ran.nextInt(height);
			p.drawOval(x, y, 10, 10);
		}
		// 画直线
		for (int i = 0; i < 50; i++) {
			p.setColor(getRandomColor());
			int y1 = ran.nextInt(width);
			int y2 = ran.nextInt(height);
			p.drawLine(0, y1 + 10, width, y2);
		}
		// 写字
		String code = "";
		p.setFont(new Font("Comic Sans MS", 1, 300));
		String s = "";
		for (int i = 0; i < 5; i++) {
			s = arrs[ran.nextInt(arrs.length)];
			code = code + s;
			p.setColor(getRandomColor());
			p.drawString(s, i * 150, 240);
		}
		HttpSession session = request.getSession();
		session.setAttribute("code", code);
		System.out.println(code);

		// 画五角星
		int m1 = 400;
		int n1 = 20;
		int m2 = 340;
		int n2 = 60;
		int m3 = 460;
		int n3 = 60;
		int m4 = 360;
		int n4 = 110;
		int m5 = 440;
		int n5 = 110;

		p.setColor(getRandomColor());
		p.drawLine(m1, n1, m4, n4);
		p.drawLine(m1, n1, m5, n5);
		p.drawLine(m2, n2, m3, n3);
		p.drawLine(m2, n2, m5, n5);
		p.drawLine(m3, n3, m4, n4);
		// 通过流把这个画相应给客户
		ImageIO.write(image, "PNG", response.getOutputStream());

	 }
	public Color getRandomColor() {
		Random ran = new Random();
		int r = ran.nextInt(255);
		int g = ran.nextInt(255);
		int b = ran.nextInt(255);
		return new Color(r, g, b);
	}
	
	

}
